package com.zyh.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import com.xiaoleilu.hutool.util.ObjectUtil;
import com.zyh.pojo.BasicAccountServicePojo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * xml 转 bean
 * bean 转 xml
 *
 * @author Xuhp 2020/12/3
 **/
@SuppressWarnings("unchecked")
public class XstreamKit {
    /**
     * Description:java 实体对象转换成xml
     *
     * @param obj 待转换的实体对象
     * @return 转换后的字符串
     */
    public static String vo2Xml(Object obj) {
        XStream xstream = new XStream();

        xstream.processAnnotations(obj.getClass());

        String data = xstream.toXML(obj);
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + data;
    }

    /**
     * 将 xml文本映射为 Java 实体对象
     *
     * @param xmlStr 待转换的 字符串
     * @param cls    待映射的 java 实体对象
     * @param <T>    转换后的实体对象
     * @return 实体对象
     */
    public static <T> T xml2Vo(String xmlStr, Class<T> cls) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(cls);
        return (T) xstream.fromXML(xmlStr);
    }


    /**
     * 模拟通过文本获取response文件的body部分
     */
    public static Map<String, Object> getBodyFromText(String text) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (text == null || text.isEmpty()) {
            map.put("error", true);
            map.put("bsnSrlNo", RandomNumber.productBsnSrNo());
            map.put("message","ESB返回为空字符串");
            return map;
        }
        try {
            Document doc = DocumentHelper.parseText(text);
            Element service = doc.getRootElement();
            String rtmCode = service.element("SysHead").element("RetInfArry").elementText("RetCd");
            String svcCd = service.element("SysHead").elementText("SvcCd");
            String scnCd = service.element("SysHead").elementText("ScnCd");
            if ("000000".equals(rtmCode) || "0000".equals(rtmCode)) {
                //卡信息查询--基本账户查询业务
                if ("30033000201".equals(svcCd + scnCd)) {
                    BasicAccountServicePojo xmlServicePojo = XstreamKit.xml2Vo(text, BasicAccountServicePojo.class);
                    if (ObjectUtil.isNotNull(xmlServicePojo.getBody())) {
                        map.put("success", xmlServicePojo.getBody());
                    } else {
                        map.put("success", "");
                    }
                    //核心记账-无折客户帐转内部帐业务
                } else if ("30011000401".equals(svcCd + scnCd)) {
                    map.put("success", true);
                    String txnSrlNo = service.element("SysHead").elementText("PvdrSrlNo");
                    map.put("txnSrlNo", txnSrlNo);
                    //核心记账-无折客户帐转内部帐冲正业务
                } else if ("30014000102".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30011000403".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30011000101".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30014000101".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30041000803".equals(svcCd + scnCd)) {
                    map.put("success", true);
                    String cnlSrlNo = service.element("Body").elementText("CnlSrlNo");
                    map.put("cnlSrlNo", cnlSrlNo);
                } else if ("30063000101".equals(svcCd + scnCd)) {
                    String srlSt = service.element("Body").elementText("SrlSt");
                    if("1".equals(srlSt.trim())){
                        map.put("srlSt", "90");
                        map.put("srlStName","转账成功");
                    }else if("2".equals(srlSt.trim())){
                        map.put("srlSt", "91");
                        map.put("srlStName","转账失败");
                    }else if("3".equals(srlSt.trim())){
                        map.put("srlSt", "93");
                        map.put("srlStName","转账处理中");
                    }
                    map.put("success", true);
                } else if ("30013000101".equals(svcCd + scnCd)) {
                    map.put("success", true);
                    String cstNm = service.element("Body").elementText("CstNm");
                    map.put("cstNm", cstNm.trim());
                    String acctOpnOtltsNo = service.element("Body").elementText("AcctOpnOtltsNo");
                    map.put("acctOpnOtltsNo", acctOpnOtltsNo.trim());
                } else if ("30033000207".equals(svcCd + scnCd)) {
                    map.put("success", true);
                    String rCBMbrBnkNm = service.element("Body").element("BnkInfArrys").element("BnkInfArry").elementText("RCBMbrBnkNm");
                    map.put("rCBMbrBnkNm", rCBMbrBnkNm);
                } else if ("30014000113".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30011000412".equals(svcCd + scnCd)) {
                    map.put("success", true);
                } else if ("30041000804".equals(svcCd + scnCd)) {
                    map.put("success", true);
                }
            } else {
                map.put("error", true);
                String txnSrlNo = service.element("SysHead").elementText("PvdrSrlNo");
                map.put("txnSrlNo", txnSrlNo);
            }
            String message = service.element("SysHead").element("RetInfArry").elementText("RetMsg");
            map.put("message", message);
            map.put("retCode", rtmCode);
        } catch (DocumentException e) {
            map.put("error", e.getMessage());
            e.printStackTrace();
        }

        return map;


    }

}